local japi = require 'jass.japi'
local jass = require 'jass.common'
local console = ac.console

load_fdf [[
    Frame "BACKDROP" "expbackdrop" {
        DecorateFileNames,
        BackdropTileBackground,
        BackdropBackground  "EscMenuEditBoxBackground",
        BackdropCornerFlags "UL|UR|BL|BR|T|L|B|R",
        BackdropCornerSize  0.0125,
        BackdropBackgroundSize  0.256,
        BackdropBackgroundInsets 0.005 0.005 0.005 0.005,
        BackdropEdgeFile  "EscMenuEditBoxBorder",
        BackdropBlendAll,
    }
]]

local amorIconFiles = {
    ['城墙'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-fortified.blp]],
    ['大型'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-large.blp]],
    ['普通'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-small.blp]],
    ['神圣'] = [[UI\Widgets\Console\Human\infocard-armor-hero.blp]],
    ['未知'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-small.blp]],
    ['无装甲']=[[UI\Widgets\Console\Human\infocard-neutral-armor-unarmored.blp]],
    ['小型'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-small.blp]],
    ['英雄'] = [[UI\Widgets\Console\Human\infocard-armor-hero.blp]],
    ['中型'] = [[UI\Widgets\Console\Human\infocard-neutral-armor-medium.blp]],
}

local attackIconFiles = {
    ['普通'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-melee.blp]],
    ['穿刺'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-piercing.blp]],
    ['法术'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-melee.blp]],
    ['攻城'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-siege.blp]],
    ['混乱'] = [[UI\Widgets\COnsole\Human\infocard-neutral-attack-chaos.blp]],
    ['近战'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-melee.blp]],
    ['魔法'] = [[UI\Widgets\COnsole\Human\infocard-neutral-attack-magic.blp]],
    ['未知'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-melee.blp]],
    ['英雄'] = [[UI\Widgets\Console\Human\infocard-neutral-attack-hero.blp]],
}
local attrIconFiles = {
    ['力量'] = [[UI\Widgets\Console\Human\infocard-heroattributes-str.blp]],
    ['敏捷'] = [[UI\Widgets\Console\Human\infocard-heroattributes-agi.blp]],
    ['智力'] = [[UI\Widgets\Console\Human\infocard-heroattributes-int.blp]],
}

local info = {
    ['经验条']   = class.ui_base.convert(japi.SimpleFrameFindByName("SimpleHeroLevelBar", 0),nil, nil, true),
    ['经验进度'] = class.ui_base.convert(japi.SimpleFrameFindByName("SimpleProgressIndicator", 0),nil, nil, true),
    ['经验文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("SimpleClassValue", 0),nil, nil, true),
    ['英雄称谓'] = class.ui_base.convert(japi.SimpleFontStringFindByName("SimpleNameValue", 0),nil, nil, true),
    ['建筑名字'] = class.ui_base.convert(japi.SimpleFontStringFindByName("SimpleBuildingNameValue", 1),nil, nil, true),
    ['科多名字'] = class.ui_base.convert(japi.SimpleFontStringFindByName("SimpleHoldNameValue", 2),nil, nil, true),
    ['力量文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroStrengthLabel", 6),nil, nil, true),
    ['力量数值'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroStrengthValue", 6),nil, nil, true),
    ['敏捷文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroAgilityLabel", 6),nil, nil, true),
    ['敏捷数值'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroAgilityValue", 6),nil, nil, true),
    ['智力文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroIntellectLabel", 6),nil, nil, true),
    ['智力数值'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconHeroIntellectValue", 6),nil, nil, true),
    ['属性图标'] = class.ui_base.convert(japi.SimpleFrameFindByName("SimpleInfoPanelIconHero", 6),nil, nil, true),
    ['攻击图标'] = class.ui_base.convert(japi.SimpleTextureFindByName("InfoPanelIconBackdrop", 0),nil, nil, true),
    ['攻击数值'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconLabel", 0),nil, nil, true),
    ['攻击文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconValue", 0),nil, nil, true),
    ['护甲图标'] = class.ui_base.convert(japi.SimpleTextureFindByName("InfoPanelIconBackdrop", 2),nil, nil, true),
    ['护甲文本'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconLabel", 2),nil, nil, true),
    ['护甲数值'] = class.ui_base.convert(japi.SimpleFontStringFindByName("InfoPanelIconValue", 2),nil, nil, true),
    -- ['英雄图标0']   = class.ui_base.convert(japi.FrameGetHeroBarButton(0), nil, nil, true),
    -- ['英雄图标1']   = class.ui_base.convert(japi.FrameGetHeroBarButton(1), nil, nil, true),
    -- ['英雄图标2']   = class.ui_base.convert(japi.FrameGetHeroBarButton(2), nil, nil, true),
    -- ['英雄图标3']   = class.ui_base.convert(japi.FrameGetHeroBarButton(3), nil, nil, true),
    -- ['英雄图标4']   = class.ui_base.convert(japi.FrameGetHeroBarButton(4), nil, nil, true),
    -- ['英雄图标5']   = class.ui_base.convert(japi.FrameGetHeroBarButton(5), nil, nil, true),
    -- ['英雄图标6']   = class.ui_base.convert(japi.FrameGetHeroBarButton(6), nil, nil, true),

}


--全部隐藏 
for k,v in pairs(info) do 
    japi.FrameClearAllPoints(v.handle)
    v:set_position(1920, 1080)
end

-- buff 栏干掉
ac.wait(0, function ()
    for i = 0, 7, 1 do 
        local handle = japi.FrameGetBuffButton(i)
        if handle ~= 0 then 
            japi.FrameSetPoint(handle, 8, japi.GetGameUI(), 8, 0.3, 0.16)
        end 
    end 
    for i = 1, 6, 1 do 
        local handle = japi.FrameGetHeroBarButton(i)
        if handle ~= 0 then 
            japi.FrameClearAllPoints( handle )
            japi.FrameSetPoint(handle, 8, japi.GetGameUI(), 8, -0.8, 0.0)
        end 
    end 
    --japi.FrameShow( japi.FrameFindByName("PauseButton", 0)    , false)
    --japi.FrameShow( japi.FrameFindByName("SaveGameButton", 0) , false)
    --japi.FrameShow( japi.FrameFindByName("LoadGameButton", 0) , false)
    --jass.PauseGame(false)
end)


local tips = {}
tips['攻击'] = function(hero,button)
    local tip = ''
    tip = ('%s|n总攻击力：%0.f'):format(tip,hero:get('攻击'))
    tip = ('%s|n基础攻击：%0.f'):format(tip,hero:get('基础攻击'))
    tip = ('%s|n额外攻击：%0.f'):format(tip,hero:get('额外攻击'))
    tip = ('%s|n攻击距离：%0.f'):format(tip,hero:get('攻击距离'))
    tip = ('%s|n攻击间隔：%0.1f'):format(tip,hero:get('攻击间隔'))
    tip = ('%s|n攻击速度：%0.f%%'):format(tip,hero:get('攻击速度'))
    return tip
end

tips['护甲'] = function(hero)
    if hero:has_restriction '无敌' then
        return '|cffff0000无敌的|r'
    end
    local def = hero:get('护甲')
    local tip = ('护甲：%0.f'):format(def)
    if def>0 then
        tip = ('%s|n伤害修正：%0.1f%%'):format(tip,def/(100 + def)*100)
    else
        tip = ('%s|n伤害修正：0%%'):format(tip)
    end
    return tip
end

tips['魔抗'] = function(hero)
    if hero:has_restriction '无敌' then
        return '|cffff0000无敌的|r'
    end
    local def = hero:get('魔抗')
    local tip = ('魔抗：%0.f'):format(def)
    if def>0 then
        tip = ('%s|n伤害修正：%0.1f%%'):format(tip,def/(100 + def)*100)
    else
        tip = ('%s|n伤害修正：0%%'):format(tip)
    end
    return tip
end



local main_attr = ' - |cffffff00主要属性|r|n - 每点提升0.5%所有伤害|n'
local main_attr = ''
tips['属性'] = function(hero)
    local tip = ''
    local s = {}
    s[#s + 1] = '力量:|n'
    if hero.main_attribute == '力量' then 
        s[#s + 1] = main_attr
    end 

    s[#s + 1] = ' - 每点提升8生命上限|n - 每点提升1生命恢复|n'
    s[#s + 1] = '敏捷:|n'
    if hero.main_attribute == '敏捷' then 
    s[#s + 1] = main_attr
    end 
    
    s[#s + 1] = ' - 每点提升0.1%物理伤害|n'
    s[#s + 1] = ' 智力:|n'
    if hero.main_attribute == '智力' then 
        s[#s + 1] = main_attr
    end 
    s[#s + 1] = ' - 每点提升0.1%魔法伤害|n'
    
    tip = table.concat(s)
    return tip
end


local unitdetail = ac.console.unitdetail:add_panel('',0,0)
unitdetail:set_level(9)
unitdetail.buttons = {}
unitdetail.title = unitdetail:add_text('名字',(1920 - 450)/2,870,450,20,13,5)

--攻击按钮/护甲按钮/魔抗按钮
local names = {'攻击','护甲','魔抗'}
local icons = {attackIconFiles['英雄'],amorIconFiles['英雄'],amorIconFiles['无装甲']}
local color = {0xffffff00,0xffffff00,0xffffff00}
for a,name in ipairs(names) do
    local icon = icons[a]
    local button = unitdetail:add_button(icon,755,935 + (a-1)*48,48,48)
    button.name = name
    button.title = button:add_text(name..':',50,3,150,20,8,4)
    button.text = button:add_text('1000',55,23,150,20,8,4)
    
    button.title:set_color(color[a])

    table.insert(unitdetail.buttons,button)
end


local herodetail = ac.console.herodetail:add_panel('',0,0)
herodetail:set_level(9)
herodetail.buttons = {}
--herodetail:add_frame('expbackdrop',745,895,450,40)
--herodetail:add_model([[UI\Feedback\XpBar\XpBarConsole.mdx]],745,895,450,40)

local frame1 = herodetail:add_frame('tooltip_backdrop1',745,900,435,30)
local frame2 = frame1:add_panel('',5,4,frame1.w-10,frame1.h-10)
frame2:add_panel([[ui\expbar.tga]])
frame2.max_w = frame2.w
frame2:set_view_port(true)

herodetail.exp = frame2
herodetail.exp_name = frame1:add_text('',0,0,nil,nil,9,5)

--三维按钮
local names = {'力量','敏捷','智力'}
local color = {0xffff0000,0xff00ff00,0xff00ffff}
for a,name in ipairs(names) do
    local icon = attrIconFiles[name]
    local button = herodetail:add_button(icon,975,935 + (a-1)*48,48,48)
    button.name = name
    button.title = button:add_text(name..':',50,3,150,20,8,4)
    button.text = button:add_text('1000',55,23,150,20,8,4)
    
    button.title:set_color(color[a])

    table.insert(herodetail.buttons,button)
end

local function show_tip(button)
    local hero = ac.unit.localunit()
    if hero then
        local func = tips[button.name] or tips['属性']
        local tip = func and func(hero)
        button:tooltip(button.name,tip)
    end
end

function unitdetail:on_button_mouse_enter(button)
    show_tip(button)
end

function herodetail:on_button_mouse_enter(button)
    show_tip(button)
end



local function update_attr(unit,key)
    local key1 = ('基础%s'):format(key)
    local key2 = ('额外%s'):format(key)
    local key3 = ('基础%s%%'):format(key)
    local base = math.floor(unit:get(key1))
    local ext = math.floor(unit:get(key2))
    local rate = math.floor(unit:get(key3))
    --local name = ("%s：%0.f%%"):format(key,rate)
    if unit:has_restriction '无敌' and (key=='护甲' or key=='魔抗') then
        return '|cffff0000无敌的|r'
    end
    local text = ''
    base = format_number1(base)
    if ext > 0 then 
        text = ("%s|cff00ff00 + %s|r"):format(base, format_number1(ext))
    elseif ext<0 then
        text = ("%s|cffff0000 - %s|r"):format(base, format_number1(-ext))
    else
        text = base
    end
    return key,text
end



local function update(unit)
    local unit = unit or ac.unit.localunit()
    if not unit then
        return
    end
    unitdetail.title:set_text(unit.name)
    
    --攻防
    for _,button in ipairs(unitdetail.buttons) do
        local name,text = update_attr(unit,button.name)
        button.title:set_text(name)
        button.text:set_text(text)
    end

    --三维
    for _,button in ipairs(herodetail.buttons) do
        local name,text = update_attr(unit,button.name)
        button.title:set_text(name)
        button.text:set_text(text)
    end

    --经验
    local min = unit.exp_need[unit.level] or 0
    local max = unit.exp_need[unit.level+1]
    if max then
        local xp1 = unit.exp - min
        local xp2 = max - min
        local rate = xp1/xp2
        herodetail.exp:set_width(herodetail.exp.max_w * rate + 0.01)
        herodetail.exp_name:set_text(('等级%s [%0.f/%0.f]'):format(unit.level,xp1,xp2))
    else
        herodetail.exp:set_width(herodetail.exp.max_w)
        herodetail.exp_name:set_text(('等级%s [已满级]'):format(unit.level))
    end
end

ac.console:event "控制台-刷新" (function ()
    update()
end)

ac.message:event '选择单位' (function(_, handle)
    local unit = ac.unit(handle)
    update(unit)
end)


return {
    unitdetail = unitdetail,
    herodetail = herodetail,
}